package com.wmyg.antifake.utils;

import com.wmyg.antifake.bean.UnitInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Slf4j
public class ExcelUtil {

    private static String PREFIX = "https://minip.weimengyungu.cn/antifake/track/item/r?i=";

    //写入Excel
    public static  boolean writeExcel(List<UnitInfo> list, Map<String,List<UnitInfo>> map,String excelPath){
        try {
            System.out.println(excelPath);
            File exportFile=new File(excelPath);
            Workbook workBook ;
            if (!exportFile.getParentFile().exists()) {
                exportFile.getParentFile().mkdirs();
            }
            if(!exportFile.exists()){
                exportFile.createNewFile();
            }
            int recordSize = list.size();
            FileOutputStream out = new FileOutputStream(exportFile);

//            workBook = new HSSFWorkbook();
            workBook = new SXSSFWorkbook(100);
            CellStyle cellStyle = workBook.createCellStyle();
            cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
            cellStyle.setFillPattern(FillPatternType.DIAMONDS);
            cellStyle.setFillForegroundColor(HSSFColor.RED.index);

            Sheet sheet =  workBook.createSheet("生成二维码信息");
            UnitInfo unitInfo = list.get(0);
            Row row0 = sheet.createRow(0);
            row0.setRowStyle(cellStyle);
            row0.createCell(0).setCellValue("商品生产时间");
            row0.createCell(1).setCellValue("商品编号");
            row0.createCell(2).setCellValue("生产批次");
            row0.createCell(3).setCellValue("生产环境");
            row0.createCell(4).setCellValue("原料批次");
            Row row1=sheet.createRow(1);
            row1.createCell(0).setCellValue(unitInfo.getProductTime());
            row1.createCell(1).setCellValue(unitInfo.getItemNo());
            row1.createCell(2).setCellValue(unitInfo.getEnterBatch());
            row1.createCell(3).setCellValue(unitInfo.getEnvironment());
            row1.createCell(4).setCellValue(unitInfo.getMaterialBatch());

           if(map.get("箱").size()!=0){
               createQrcode(workBook,"箱",map.get("箱"),cellStyle);
           }
            if(map.get("盒").size()!=0){
                createQrcode(workBook,"盒",map.get("盒"),cellStyle);
            }
            if(map.get("包").size()!=0){
                createQrcode(workBook,"包",map.get("包"),cellStyle);
            }

            workBook.write(out);
            out.flush();
            out.close();

            workBook.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //写入Excel
    public static  boolean writeExcel(UnitInfo unitInfo, Map<String, List<Dozen>> dozenMap,String excelPath, String dateString){
        try {
            System.out.println(excelPath);
            File exportFile=new File(excelPath);
            Workbook workBook ;
            if (!exportFile.getParentFile().exists()) {
                exportFile.getParentFile().mkdirs();
            }
            if(!exportFile.exists()){
                exportFile.createNewFile();
            }
//            int recordSize = list.size();
            FileOutputStream out = new FileOutputStream(exportFile);

//            workBook = new HSSFWorkbook();
            workBook = new SXSSFWorkbook(100);
            CellStyle cellStyle = workBook.createCellStyle();
            cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
            cellStyle.setFillPattern(FillPatternType.DIAMONDS);
            cellStyle.setFillForegroundColor(HSSFColor.RED.index);

            Sheet sheet =  workBook.createSheet("生成二维码信息");
//            UnitInfo unitInfo = list.get(0);
            Row row0 = sheet.createRow(0);
            row0.setRowStyle(cellStyle);
            row0.createCell(0).setCellValue("商品生产时间");
            row0.createCell(1).setCellValue("商品编号");
            row0.createCell(2).setCellValue("生产批次");
            row0.createCell(3).setCellValue("生产环境");
            row0.createCell(4).setCellValue("原料批次");
            Row row1=sheet.createRow(1);
            row1.createCell(0).setCellValue(unitInfo.getProductTime());
            row1.createCell(1).setCellValue(unitInfo.getItemNo());
            row1.createCell(2).setCellValue(unitInfo.getEnterBatch());
            row1.createCell(3).setCellValue(unitInfo.getEnvironment());
            row1.createCell(4).setCellValue(unitInfo.getMaterialBatch());

            if(null != dozenMap.get("箱") && dozenMap.get("箱").size()!=0){
                createQrcode(workBook,"箱",dozenMap.get("箱"),cellStyle,unitInfo,dateString);
            }
            if(null != dozenMap.get("盒") && dozenMap.get("盒").size()!=0){
                createQrcode(workBook,"盒",dozenMap.get("盒"),cellStyle,unitInfo,dateString);
            }
            if(null != dozenMap.get("包") && dozenMap.get("包").size()!=0){
                createQrcode(workBook,"包",dozenMap.get("包"),cellStyle,unitInfo,dateString);
            }

            workBook.write(out);
            out.flush();
            out.close();

            workBook.close();

        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            for (StackTraceElement s :
                    e.getStackTrace()) {
                log.info(s.toString());
            }
            return false;
        }
        return true;
    }

   static  void createQrcode( Workbook workBook,String qrcodeName ,List<UnitInfo> list,CellStyle cellStyle){
        Sheet sheet =  workBook.createSheet(qrcodeName+"二维码");
        int rowIndex = 1 ;
        for (int i = 0; i < list.size(); i++) {
            UnitInfo item = list.get(i);
            if ((i + 1) % 60000 == 0) {
                sheet = workBook.createSheet(qrcodeName+"二维码"+i);
                rowIndex = 1 ;
            }
            if(rowIndex == 1){
                //设置表头
                Row row00 = sheet.createRow(0);
                row00.setRowStyle(cellStyle);
                row00.createCell(0).setCellValue("二维码");
                row00.createCell(1).setCellValue("包装级别");
            }
            Row row = sheet.createRow(rowIndex);
            if(qrcodeName.equals("包")){
                row.createCell(0).setCellValue(item.getqR());
            }else{
                row.createCell(0).setCellValue(PREFIX + item.getqR());
            }

            row.createCell(1).setCellValue(item.getPackLevel());
            rowIndex++;
        }
    }

    static  void createQrcode( Workbook workBook,String qrcodeName,List<Dozen> list,CellStyle cellStyle, UnitInfo unitInfo, String dateString){
        for (int i = 0; i < list.size(); i++) {
            Dozen dozen = list.get(i);
            Sheet sheet = workBook.createSheet(qrcodeName+"二维码"+i);
                //设置表头
            Row row00 = sheet.createRow(0);
            row00.setRowStyle(cellStyle);
            row00.createCell(0).setCellValue("二维码");
            row00.createCell(1).setCellValue("包装级别");
            int rowIndex = 1;
            for (String s :
                    dozen.getQrs()) {
                Row row = sheet.createRow(rowIndex);
                String wholeQR = MaterialCodeUtil.montageGS1(unitInfo.getItemNo(), dateString, unitInfo.getEnterBatch(), dozen.getLevel(), dozen.getDozenId(), s);
                if(qrcodeName.equals("包")){
                    row.createCell(0).setCellValue(wholeQR);
                }else{
                    row.createCell(0).setCellValue(PREFIX + wholeQR);
                }

                row.createCell(1).setCellValue(dozen.getLevel());
                rowIndex++;
            }
        }
    }
}

